import { instance as request } from '@/utils/request';
import type { User, Data, UserInfo, PatientList, Patient } from '@/types/user';

// 封装登录请求
export const loginByPassword = async (mobile: string, password: string) => {
  // 1 请求参数的处理

  // 具体的请求返回数据处理
  const res = await request.post<User, Data<User>>('/login/password', {
    mobile,
    password,
  });

  // 2 返回数据的格式特殊处理
  return res.data;
};
// 获取验证码
export const getSMSCode = async (mobile: string, type: string) => {
  const res = await request.get('/code', { params: { mobile, type } });
  console.log('===>', res);
  return res.data;
};

// 验证码登录请求
export const loginByCode = async (mobile: string, code: string) => {
  const res = await request.post<User, Data<User>>('/login', { mobile, code });
  return res.data;
};

// 获取用户详细信息
export const getUserInfo = async () => {
  const res = await request.get<UserInfo, Data<UserInfo>>('/patient/myUser');
  return res.data;
};

// 获取档案信息-患者信息
export const getPatientList = async () => {
  const res = await request.get<PatientList, Data<PatientList>>(
    '/patient/mylist'
  );
  return res.data;
};

// 新增患者信息
export const addPatient = async (patient: Patient) => {
  const res = await request.post('/patient/add', patient);
  return res.data;
};

// 更新患者信息
export const editPatient = async (patient: Patient) => {
  const res = await request.put('/patient/update', patient);
  return res.data;
};

// 删除患者
export const delPatient = async (id: string) => {
  const res = await request.delete(`/patient/del/${id}`);
  return res.data;
};

// 获取患者信息
export const getPatient = async (id: string) => {
  const res = await request.get<Patient, Data<Patient>>(`/patient/info/${id}`);
  return res.data;
};

// QQ登录（openId 登录）
export const loginByQQ = async (openId: string) => {
  const res = await request.post<User, Data<User>>('/login/thirdparty', {
    openId,
    source: 'qq',
  });
  return res.data;
};

// 绑定手机号
export const bindMobile = async (data: {
  openId: string;
  mobile: string;
  code: string;
}) => {
  const res = await request.post<User, Data<User>>('/login/binding', data);
  return res.data;
};

// 解绑
export const unbound = async (openId: string) => {
  // 发请求
  const res = await request.put('/unbound/' + openId);
  return res.data;
};
